<%--
  Created by IntelliJ IDEA.
  User: Stiofan
  Date: 2017/1/19
  Time: 16:25
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="/WEB-INF/jsp/common/tags.jsp" %>

<aos:html title="房产租赁信息管理" base="http" lib="ext">
    <aos:body>
    </aos:body>
</aos:html>
<aos:onready>
    <aos:viewport layout="fit">
        <aos:panel layout="column" columnWidth="1" border="false" autoScroll="true">
            <aos:docked dock="top" columnWidth="1" layout="column" border="true" height="110">
                <aos:formpanel id="fcjcxxForm" layout="column" columnWidth="1">
                    <aos:fieldset title="房产基础信息" collapsible="false">
                        <aos:textfield id="fcbh" name="fcbh" fieldLabel="房产编号" columnWidth="0.25"
                                       onenterkey="queryFcjcxxByFcbh" star="true" allowBlank="false"/>
                        <aos:textfield id="fcmc" name="fcmc" fieldLabel="房产名称" columnWidth="0.25" readOnly="true"/>
                        <aos:textfield id="jzmj" name="jzmj" fieldLabel="建筑面积(平米)" columnWidth="0.25" readOnly="true"
                                       regex="/^[0-9]+([.]{1}[0-9]{1,2})?$/" regexText="必输输入数字并且保留至多两位小数"
                                       msgTarget="under"/>
                        <aos:combobox id="fcytDm" name="fcytDm" fieldLabel="房产用途" columnWidth="0.25" dicField="fcytDm"
                                      readOnly="true"/>
                        <aos:combobox id="fcjgDm" name="fcjgDm" fieldLabel="房产结构" columnWidth="0.25" dicField="fcjgDm"
                                      readOnly="true"/>
                        <aos:textfield id="fczyz" name="fczyz" fieldLabel="房产总原值" columnWidth="0.25" readOnly="true"
                                       regex="/^[0-9]+([.]{1}[0-9]{1,2})?$/" regexText="必输输入数字并且保留至多两位小数"
                                       msgTarget="under"/>
                        <aos:textfield id="zldz" name="zldz" fieldLabel="坐落地址" columnWidth="0.5" readOnly="true"/>
                    </aos:fieldset>
                </aos:formpanel>
            </aos:docked>
            <aos:gridpanel id="fcsyqGrid" forceFit="true" layout="fit" columnWidth="1" width="2000"
                           url="fcczxxglService.queryFcczxxByFcbh" hidePagebar="true" autoScroll="true"
                           onitemdblclick="makeSure_fcsyqxx">
                <aos:column header="登记序号" dataIndex="djxh" hidden="true"/>
                <aos:column header="房产使用权UUID" dataIndex="fcsyquuid" hidden="true"/>
                <aos:column header="房产编号" dataIndex="fcbh" hidden="true"/>
                <aos:column header="承租人识别号" dataIndex="nsrsbh" align="center"/>
                <aos:column header="承租人名称" dataIndex="nsrmc" align="center"/>
                <aos:column header="使用人类型" dataIndex="fcsyrlxDm" rendererField="fcsyrlxDm" hidden="true"/>
                <aos:column header="租赁起始日期" dataIndex="fcsyqQsrq" align="center" type="date" format="Y-m-d"/>
                <aos:column header="租赁终止日期" dataIndex="fcsyqZzrq" align="center" type="date" format="Y-m-d"/>
                <aos:column header="租赁面积" dataIndex="fcsyqYsmj" align="right" type="number" format="0.00"/>
                <aos:column header="租赁金额(月)" dataIndex="fcsyqYsje" align="right" type="number" format="0.00"/>
                <aos:column header="租赁金额(年)" dataIndex="fcsyqZlje" align="right" type="number" format="0.00"/>
            </aos:gridpanel>
            <aos:docked dock="bottom" ui="footer" columnWidth="1" layout="column" border="false" height="270">
                <aos:formpanel id="ryjcxxForm" layout="column" columnWidth="1">
                    <aos:fieldset title="承租人信息" collapsible="false">
                        <aos:hiddenfield id="djxh" name="djxh"/>
                        <aos:textfield id="nsrsbh" name="nsrsbh" fieldLabel="承租人识别号" columnWidth="0.25"
                                       onenterkey="queryRyjcxx" star="true" allowBlank="false"/>
                        <aos:hiddenfield id="shxydm" name="shxydm" fieldLabel="社会信用代码" columnWidth="0.25"
                                         onenterkey="queryRyjcxx"/>
                        <aos:hiddenfield id="zzjgdm" name="zzjgdm" fieldLabel="组织机构代码" columnWidth="0.25"
                                         onenterkey="queryRyjcxx"/>
                        <aos:textfield id="nsrmc" name="nsrmc" fieldLabel="承租人名称" columnWidth="0.25" readOnly="true"/>
                        <aos:textfield id="lxrxm" name="lxrxm" fieldLabel="联系人姓名" columnWidth="0.25" readOnly="true"/>
                        <aos:combobox id="lxrsfzjlxDm" name="lxrsfzjlxDm" fieldLabel="证件类型" dicField="sfzjlxDm"
                                      columnWidth="0.25" dicFilter="201" readOnly="true"/>
                        <aos:textfield id="lxrsfzjhm" name="lxrsfzjhm" fieldLabel="证件号码" columnWidth="0.25"
                                       readOnly="true"/>
                        <aos:textfield id="lxrgddh" name="lxrgddh" fieldLabel="固定电话" columnWidth="0.25"
                                       readOnly="true"/>
                        <aos:textfield id="lxryddh" name="lxryddh" fieldLabel="移动电话" columnWidth="0.25"
                                       readOnly="true"/>
                        <aos:textfield id="lxrdz" name="lxrdz" fieldLabel="联系人地址" columnWidth="0.75" readOnly="true"/>
                    </aos:fieldset>
                </aos:formpanel>
                <aos:formpanel id="fcsyqdjxxForm" layout="column" columnWidth="1">
                    <aos:fieldset title="房产租赁信息" collapsible="false">
                        <aos:hiddenfield id="fcsyquuid" name="fcsyquuid"/>
                        <aos:hiddenfield id="fcsyrlxDm" name="fcsyrlxDm"
                                         fieldLabel="使用人类型" value="05"
                                         columnWidth="0.25"/>
                        <aos:hiddenfield id="fcsyqlxDm" name="fcsyqlxDm" fieldLabel="使用权类型"
                                         columnWidth="0.25" value="02" readOnly="true"/>
                        <aos:hiddenfield id="fcsyqZsbh" name="fcsyqZsbh" fieldLabel="使用权证书编号" columnWidth="0.25"/>
                        <aos:hiddenfield id="fcsyqQdrq" name="fcsyqQdrq" fieldLabel="使用权取得日期" columnWidth="0.25"/>
                        <aos:datefield id="fcsyqQsrq" name="fcsyqQsrq" fieldLabel="租赁起始日期" columnWidth="0.25"
                                       star="true"
                                       allowBlank="false"/>
                        <aos:datefield id="fcsyqZzrq" name="fcsyqZzrq" fieldLabel="租赁终止日期" columnWidth="0.25"
                                       star="true"
                                       allowBlank="false"/>
                        <aos:textfield id="fcsyqYsmj" name="fcsyqYsmj" fieldLabel="租赁面积" columnWidth="0.25"
                                       regex="/^[0-9]+([.]{1}[0-9]{1,2})?$/" regexText="必输输入数字并且保留至多两位小数"
                                       msgTarget="under" onblur="calcForFcsyqZljeAndYzje" star="true"
                                       allowBlank="false"/>
                        <aos:textfield id="fcsyqYsje" name="fcsyqYsje" fieldLabel="租赁金额(月)" columnWidth="0.25"
                                       regex="/^[0-9]+([.]{1}[0-9]{1,2})?$/" regexText="必输输入数字并且保留至多两位小数"
                                       msgTarget="under" onblur="calcForFcsyqZljeAndYzje" star="true"
                                       allowBlank="false"/>
                        <aos:textfield id="fcsyqZlje" name="fcsyqZlje" fieldLabel="租赁金额(年)" columnWidth="0.25"
                                       regex="/^[0-9]+([.]{1}[0-9]{1,2})?$/" regexText="必输输入数字并且保留至多两位小数"
                                       msgTarget="under" readOnly="true" star="true" allowBlank="false"/>
                        <aos:hiddenfield id="fcsyqJmxz" name="fcsyqJmxz" fieldLabel="减免性质" columnWidth="0.25"/>
                        <aos:hiddenfield id="fcsyqMsmj" name="fcsyqMsmj" fieldLabel="使用权免税面积" columnWidth="0.25"
                                         regex="/^[0-9]+([.]{1}[0-9]{1,2})?$/" regexText="必输输入数字并且保留至多两位小数"
                                         msgTarget="under"/>
                        <aos:hiddenfield id="fcsyqMsje" name="fcsyqMsje" fieldLabel="使用权免税金额" columnWidth="0.25"
                                         regex="/^[0-9]+([.]{1}[0-9]{1,2})?$/" regexText="必输输入数字并且保留至多两位小数"
                                         msgTarget="under"/>
                        <aos:textfield id="fcsyqYzje" name="fcsyqYzje" fieldLabel="租赁原值" columnWidth="0.25"
                                       regex="/^[0-9]+([.]{1}[0-9]{1,2})?$/" regexText="必输输入数字并且保留至多两位小数"
                                       msgTarget="under" star="true" allowBlank="false" readOnly="true"/>
                    </aos:fieldset>
                </aos:formpanel>
                <aos:window id="zfFcsyqdjxxWin" title="作废房产租赁信息" width="1000" maxHeight="460">
                    <aos:formpanel id="zfFcsyqdjxxForm" layout="column" autoScroll="true">
                        <aos:textareafield id="zfyy" name="zfyy" fieldLabel="作废原因" labelWidth="60" columnWidth="1"
                                           height="200"/>
                        <aos:docked dock="bottom" ui="footer">
                            <aos:dockeditem xtype="tbfill"/>
                            <aos:dockeditem text="作废" onclick="zfFcsyqdjxxByFcsyquuid" icon="del.png"/>
                            <aos:dockeditem text="关闭" onclick="#zfFcsyqdjxxWin.close();" icon="close.png"/>
                        </aos:docked>
                    </aos:formpanel>
                </aos:window>
                <aos:panel layout="column" columnWidth="1" border="false">
                    <aos:panel layout="column" columnWidth="0.5" center="true" animate="false" border="false">
                        <aos:button id="btnAdd" text="新增" columnWidth="0.25" margin="2" onclick="addNewFcsyqdjxx"/>
                        <aos:button id="btnSave" text="保存" columnWidth="0.25" margin="2" onclick="saveFcsyqdjxx"/>
                        <aos:button id="btnDel" text="作废" columnWidth="0.25" margin="2"
                                    onclick="#zfFcsyqdjxxWin.show();"/>
                        <aos:button text="重置" columnWidth="0.25" margin="2" onclick="resetAllForms"/>
                    </aos:panel>
                </aos:panel>
            </aos:docked>
        </aos:panel>
    </aos:viewport>
    <script type="text/javascript">
        function queryFcjcxxByFcbh() {
            if (fcbh.getValue() === "") {
                AOS.err("必须输入编号进行查询");
            } else {
                if (fcbh.getValue() !== "") {
                    AOS.read(fcbh);
                }
                var params = {
                    fcbh: fcbh.getValue()
                };
                AOS.ajax({
                    params: params,
                    url: "fcjcxxcjService.queryFcjcxxByFcbh",
                    ok: function (data) {
                    	Ext.getCmp("btnAdd").setDisabled(false);
                        Ext.getCmp("btnSave").setDisabled(false);
                        Ext.getCmp("btnDel").setDisabled(false);
                        if (data.appcode === "err") {
                            AOS.err("未查询到房产基础信息，请先采集房产基础信息!");
                            Ext.getCmp("btnAdd").setDisabled(true);
                            Ext.getCmp("btnSave").setDisabled(true);
                            Ext.getCmp("btnDel").setDisabled(true); 
                            AOS.edit(fcbh);
                        } else {
                            AOS.setValues(fcjcxxForm, data);
                            queryFcczxxByFcbh();
                        }
                    }
                });
            }
        }
        function queryRyjcxx() {
            if (djxh.getValue() === "" && nsrsbh.getValue() === "") {
                AOS.err("必须输入纳税人识别号进行查询");
                return;
            }
            var params = {
                nsrsbh: nsrsbh.getValue(),
                shxydm: nsrsbh.getValue(),
                zzjgdm: nsrsbh.getValue(),
                djxh: Ext.getCmp("djxh").getValue()
            };
            AOS.ajax({
                params: params,
                url: "fcczxxglService.queryRyjcxx",
                ok: function (data) {
                    
                    if (data.appcode === "noRyxx") {
                        AOS.err("未查询到相关信息，请先进行人员基础信息采集!");
                      
                        AOS.edit(nsrsbh);
                        AOS.edit(shxydm);
                        AOS.edit(zzjgdm);
                    } else {
                        AOS.read(nsrsbh);
                        AOS.read(shxydm);
                        AOS.read(zzjgdm);
                        AOS.setValues(ryjcxxForm, data);
                    }
                }
            });

        }
        function queryFcczxxByFcbh() {
            var params = {
                fcbh: Ext.getCmp("fcbh").getValue()
            };
            fcsyqGrid_store.getProxy().extraParams = params;
            fcsyqGrid_store.reload();
        }
        function saveFcsyqdjxx() {
           
          /*   if (Number(Ext.getCmp("fcsyqMsmj").getValue()) > Number(Ext.getCmp("jzmj").getValue())) {
                AOS.err("免税面积不能大于建筑面积,请修改!");
                return;
            } */
         
            if (AOS.valid(fcsyqdjxxForm)) {
                if (Ext.getCmp("fcsyqYsmj").getValue() === "") {
                    AOS.setValue("fcsyqYsmj", 0);
                }
                if (Ext.getCmp("fcsyqMsmj").getValue() === "") {
                    AOS.setValue("fcsyqMsmj", 0);
                }
                if (Ext.getCmp("fcsyqYsje").getValue() === "") {
                    AOS.setValue("fcsyqYsje", 0);
                }
                if (Ext.getCmp("fcsyqMsje").getValue() === "") {
                    AOS.setValue("fcsyqMsje", 0);
                }
                var params = {
                    djxh: Ext.getCmp("djxh").getValue(),
                    fcbh: Ext.getCmp("fcbh").getValue(),
                    jzmj: Ext.getCmp("jzmj").getValue()
                };
                var fcysymj = 0;//定义房产已使用面积，用于累加已经登记使用权的应税面积及免税面积
                for (var i = 0; i < fcsyqGrid_store.getCount(); i++) {
                    var record = fcsyqGrid_store.getAt(i);
                    if (Ext.getCmp("fcbh").getValue() === record.data.fcbh && Ext.getCmp("fcsyquuid").getValue() !== record.data.fcsyquuid) {
                        fcysymj = fcysymj + parseFloat(record.data.fcsyqYsmj); //+ parseFloat(record.data.fcsyqMsmj);
                        //当使用人类型与使用权类型已经存在登记过的信息是，将校验是否存在日期交叉情况
                        if (Ext.getCmp("fcsyrlxDm").getValue() === record.data.fcsyrlxDm &&
                            Ext.getCmp("fcsyqlxDm").getValue() === record.data.fcsyqlxDm) {
                            if ((Date.parse(Ext.getCmp("fcsyqQsrq").getValue()) <= Date.parse(record.data.fcsyqQsrq.substr(0, 10)) &&
                                Date.parse(Ext.getCmp("fcsyqZzrq").getValue()) >= Date.parse(record.data.fcsyqQsrq.substr(0, 10))) ||
                                (Date.parse(Ext.getCmp("fcsyqQsrq").getValue()) <= Date.parse(record.data.fcsyqZzrq.substr(0, 10)) &&
                                Date.parse(Ext.getCmp("fcsyqZzrq").getValue()) >= Date.parse(record.data.fcsyqZzrq.substr(0, 10))) ||
                                (Date.parse(Ext.getCmp("fcsyqQsrq").getValue()) <= Date.parse(record.data.fcsyqQsrq.substr(0, 10)) &&
                                Date.parse(Ext.getCmp("fcsyqZzrq").getValue()) >= Date.parse(record.data.fcsyqZzrq.substr(0, 10))) ||
                                (Date.parse(Ext.getCmp("fcsyqQsrq").getValue()) >= Date.parse(record.data.fcsyqQsrq.substr(0, 10)) &&
                                Date.parse(Ext.getCmp("fcsyqZzrq").getValue()) <= Date.parse(record.data.fcsyqZzrq.substr(0, 10)))) {
                                AOS.err("针对同一使用人类型及使用权类型，不允许存在多条登记信息!");
                                return;
                            }
                        }
                    }
                }
                var fcdjzmj = parseFloat(Ext.getCmp("fcsyqYsmj").getValue()) + parseFloat(Ext.getCmp("fcsyqMsmj").getValue()) + fcysymj;//parseFloat(fcysymj);
                //校验面积
                if (fcdjzmj > parseFloat(Ext.getCmp("jzmj").getValue())) {
                	//debugger;
                	//console.log("***********************");
                    AOS.err("本次登记的使用权面积与已登记的使用权面积之和(" + fcdjzmj + ")大于房产建筑面积(" + parseFloat(Ext.getCmp("jzmj").getValue()) + "),请检查所填租赁面积是否正确!");
                    return;
                }
               /*  if (Number(Ext.getCmp("fcsyqYzje").getValue()).toFixed(2) > Number(Ext.getCmp("fczyz").getValue()).toFixed(2)) {
                    AOS.err("房产租赁原值不能大于房产总原值,请修改!");
                    return;
                } */
                AOS.ajax({
                    params: params,
                    forms: fcsyqdjxxForm,
                    url: "fcsyqdjglService.saveFcsyqdjxx",
                    ok: function (data) {
                        if (data.appcode === "suc") {
                            AOS.tip("房产租赁信息保存成功！");
                            fcsyqGrid_store.reload();
                            Ext.getCmp("btnSave").setDisabled(true);
                        } else if (data.appcode === "err") {
                            AOS.err(data.appmsg);
                        }

                    }
                });
            } else {
                AOS.err("请检查相关信息是否输入完整！");
            }
        }
        function zfFcsyqdjxxByFcsyquuid() {
        	 var record = AOS.selectone(Ext.getCmp("fcsyqGrid"));
             var params = {
            		 fcsyquuid: record.data.fcsyquuid
             };
           /*  var params = {
                fcsyquuid: Ext.getCmp("fcsyquuid").getValue()
            }; */
            AOS.ajax({
                params: params,
                forms: zfFcsyqdjxxForm,
                url: "fcczxxglService.zfFcsyqdjxxByFcsyquuid",
                ok: function (data) {
                    if (data.appcode === "suc") {
                        AOS.tip("房产租赁信息作废成功!");
                        fcsyqGrid_store.reload();
                        AOS.reset(fcsyqdjxxForm);
                        AOS.reset(ryjcxxForm);
                        AOS.edit(nsrsbh);
                        AOS.edit(shxydm);
                        AOS.edit(zzjgdm);
                    } else if (data.appcode === "err") {
                        AOS.err(data.appmsg);
                    }
                    zfFcsyqdjxxWin.close();
                }
            });
        }
        function resetAllForms() {
            AOS.reset(ryjcxxForm);
            AOS.reset(fcjcxxForm);
            AOS.reset(fcsyqdjxxForm);
            AOS.edit(nsrsbh);
            AOS.edit(shxydm);
            AOS.edit(zzjgdm);
            AOS.edit(fcbh);
            fcsyqGrid_store.removeAll();
            Ext.getCmp("btnAdd").setDisabled(true);
            Ext.getCmp("btnSave").setDisabled(true);
            Ext.getCmp("btnDel").setDisabled(true);
        }
        function makeSure_fcsyqxx() {
            var record = AOS.selectone(Ext.getCmp("fcsyqGrid"));
            AOS.reset(ryjcxxForm);
            AOS.reset(fcsyqdjxxForm);
            AOS.setValue("ryjcxxForm.djxh", record.data.djxh);
            AOS.setValue("fcsyqdjxxForm.fcsyquuid", record.data.fcsyquuid);
            queryRyjcxx();
            queryFcsyqdjxx();
        }
        function queryFcsyqdjxx() {
            var params = {
                djxh: Ext.getCmp("djxh").getValue(),
                fcbh: Ext.getCmp("fcbh").getValue(),
                fcsyquuid: fcsyquuid.getValue()
            };
            AOS.ajax({
                params: params,
                url: "fcsyqdjglService.queryFcsyqdjxxByFcsyquuid",
                ok: function (data) {
                    AOS.setValues(fcsyqdjxxForm, data);
                    if (data.fcsyqQsrq !== undefined && data.fcsyqQsrq !== "") {
                        AOS.setValue("fcsyqdjxxForm.fcsyqQsrq", Ext.Date.parse(data.fcsyqQsrq.substr(0, 10), 'Y-m-d', true));
                    }
                    if (data.fcsyqZzrq !== undefined && data.fcsyqZzrq !== "") {
                        AOS.setValue("fcsyqdjxxForm.fcsyqZzrq", Ext.Date.parse(data.fcsyqZzrq.substr(0, 10), 'Y-m-d', true));

                    }
                    if (data.fcsyqQdrq !== undefined && data.fcsyqQdrq !== "") {
                        AOS.setValue("fcsyqdjxxForm.fcsyqQdrq", Ext.Date.parse(data.fcsyqQdrq.substr(0, 10), 'Y-m-d', true));

                    }
                    calcForFcsyqZljeAndYzje();
                }
            });
        }
        function addNewFcsyqdjxx() {
            AOS.edit(nsrsbh);
            AOS.edit(shxydm);
            AOS.edit(zzjgdm);
            AOS.reset(ryjcxxForm);
            AOS.reset(fcsyqdjxxForm);
        }
        function calcForFcsyqZljeAndYzje() {
            var zlje = Ext.getCmp("fcsyqYsje").getValue() * 12;
            Ext.getCmp("fcsyqZlje").setValue(Number(zlje).toFixed(2));
            var dwyz = Ext.getCmp("fczyz").getValue() / Ext.getCmp("jzmj").getValue();
            Ext.getCmp("fcsyqYzje").setValue(Number(dwyz * Ext.getCmp("fcsyqYsmj").getValue()).toFixed(2));
        }
    </script>
    Ext.getCmp("btnAdd").setDisabled(true);
    Ext.getCmp("btnSave").setDisabled(true);
    Ext.getCmp("btnDel").setDisabled(true);
</aos:onready>
